package com.module.admin.rest.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.rest.cache.RestLogCache;
import com.module.admin.rest.dao.RestLogDao;
import com.module.admin.rest.pojo.RestInfo;
import com.module.admin.rest.pojo.RestLog;
import com.module.admin.rest.pojo.RestServer;
import com.module.admin.rest.service.RestInfoService;
import com.module.admin.rest.service.RestLogService;
import com.module.admin.rest.service.RestServerService;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameJsonUtil;
import com.system.comm.utils.FrameNoUtil;
import com.system.handler.model.ResponseCode;
import com.system.handler.model.ResponseFrame;

/**
 * rest_log的Service
 * @author yuejing
 * @date 2016-12-23 16:43:04
 * @version V1.0.0
 */
@Component
public class RestLogServiceImpl implements RestLogService {

	@Autowired
	private RestLogDao restLogDao;
	@Autowired
	private RestServerService restServerService;
	@Autowired
	private RestInfoService restInfoService;
	@Autowired
	private RestLogCache restLogCache;

	@Override
	public ResponseFrame save(String serverAddress, Date requestTime, String url, Map<String, String> params) {
		ResponseFrame frame = new ResponseFrame();
		//判断服务是否保存
		ResponseFrame rsFrame = restServerService.save(serverAddress);
		if(ResponseCode.SUCC.getCode() != rsFrame.getCode().intValue()) {
			return rsFrame;
		}
		RestServer restServer = (RestServer) rsFrame.getBody();
		//判断地址是否保存
		RestInfo restInfo = new RestInfo();
		restInfo.setRsId(restServer.getRsId());
		restInfo.setUrl(url);
		ResponseFrame riFrame = restInfoService.save(restInfo);
		if(ResponseCode.SUCC.getCode() != riFrame.getCode().intValue()) {
			return riFrame;
		}
		restInfo = (RestInfo) riFrame.getBody();
		
		//保存记录
		RestLog restLog = new RestLog();
		restLog.setRlId(FrameNoUtil.uuid());
		//接口项目编号
		restLog.setRiId(restInfo.getRiId());
		//请求时间
		restLog.setRequestTime(requestTime);
		StringBuffer dtl = new StringBuffer("请求参数为: ");
		dtl.append(FrameJsonUtil.toString(params));
		restLog.setDtl(dtl.toString());
		restLogDao.save(restLog);
		
		//计数+1
		restLogCache.plus(restLog.getRiId(), restLog.getRequestTime());
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public RestLog get(String rlId) {
		return restLogDao.get(rlId);
	}

	@Override
	public ResponseFrame pageQuery(RestLog restLog) {
		ResponseFrame frame = new ResponseFrame();
		int total = restLogDao.findRestLogCount(restLog);
		List<RestLog> data = null;
		if(total > 0) {
			data = restLogDao.findRestLog(restLog);
			for (RestLog rl : data) {
				RestInfo ri = restInfoService.get(rl.getRiId());
				rl.setUrl(ri.getUrl());
				RestServer rs = restServerService.get(ri.getRsId());
				rl.setServerAddress(rs.getServerAddress());
			}
		}
		Page<RestLog> page = new Page<RestLog>(restLog.getPage(), restLog.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public ResponseFrame delete(String rlId) {
		ResponseFrame frame = new ResponseFrame();
		restLogDao.delete(rlId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
}
